<?php


namespace app\api\controller\v13;


use app\common\cachemodel\ZhunleCourse;
use app\common\cachemodel\ZlUserVip;
use app\common\lib\exception\ApiException;
use app\common\model\ArticleCommentReply;
use app\common\model\MessageCenter;
use app\common\model\Recode;
use app\common\model\User;
use Exception as ExceptionAlias;

class Personal extends Auth2
{
    /**
     * @title
     * @method GET
     * @desc  获取我的信息
     * @return array
     * @throws ApiException
     * @throws ExceptionAlias
     */
    public function index()
    {
        if (!request()->isGet())
        {
            return show(config('code.error'), '未知操作', [], 403);
        }

        //判断用户是否绑定准了号
        $uen = (new User())->check_uen($this->user);

        $RecodeModel = new Recode();
        $MsgModel = new MessageCenter();
        $uid = $this->user['id'];

        if (!$uid)
        {
            $array = [
                'id' => null,
                'uen' => null,
                'name' => null,
                'avatar' => null,
                'sex' => null,
                'sun' => null,
                'moon' => null,
                'asc' => null,
                'msg_count' => 0,
                'vip' => null,
                'vip_status' => 1
            ];

            return show(200, '成功', $array);
        }

        $map = ['uid' => $this->user['id']];
        $rea = $RecodeModel->getSelfRecode($map);
        if ($rea)
        {
            $sex = $rea['type'] == 1 ? 1 : 2;
            $data = [
                'id' => $rea['id'],
                'uen' => $uen ?: $this->user['uen'],
                'name' => $rea['name'],
                'sex' => $sex,
                'avatar' => $rea['avatar'],
                'sun' => $rea['sun'],
                'moon' => $rea['moon'],
                'asc' => $rea['asc'],
            ];
        } else
        {
            $data = [
                'uen' => $uen ?: $this->user['uen'],
                'name' => $this->user['nickname'],
                'avatar' => $this->user['avatar'],
                // 'sex' => $this->user['sex'],
            ];
        }

        //获取用户全部未读系统消息id
        $system_msg_id = $MsgModel->getAllUserMessage(['uid' => $uid, 'type' => 1]);
        //获取用户全部未读服务消息id
        $server_msg_id = $MsgModel->getAllUserMessage(['uid' => $uid, 'type' => 2]);
        $msg_count = count($system_msg_id) + count($server_msg_id);

        // 获取回复的阅读情况
        $replyModel = new ArticleCommentReply();
        $unread_ids = $replyModel->getUnReadReply(['ucid' => $this->user['uc_id'], 'uid' => $this->user['id']]);
        if ($unread_ids)
        {
            $reply_count = count($unread_ids);
        } else
        {
            $reply_count = 0;
        }
        $array1['msg_count'] = $msg_count + $reply_count;
        $array = array_merge($data, $array1);
        //获取用户已解锁的视频数据
        $unlock_id = ZhunleCourse::getVideoUnlockStatus($this->user['id']);
        if ($unlock_id['gids'])
        {
            $array['my_course_switch'] = 1;
        } else
        {
            $array['my_course_switch'] = 0;
        }
        //获取用户开通会员记录
        $userVip = (new ZlUserVip())->getInfoById($uid);

        if ($userVip)
        {
            $array['vip'] = [
                'vip_level' => (int)$userVip['vip_level'],
                'vip_expire_date' => date('Y-m-d', $userVip['end_time']),
            ];
            $array['vip_status'] = $userVip['is_valid'] ? 2 : 3; // 2续费，3激活开通
        } else
        {
            $array['vip'] = null;
            $array['vip_status'] = 1; // 未开通
        }
        return show(200, '成功', $array);
    }
}
